import { application } from "../hcl/Application.js";
import { clipboard, TDataFormat } from "../hcl/Clipboard.js";
import { TAlign, TControlState } from "../hcl/Controls.js";
import { TForm, TOpenDialog } from "../hcl/Forms.js";
import { TFont, TFontStyle } from "../hcl/Graphics.js";
import { hcl } from "../hcl/HCL.js";
import { TColorCombobox, TCombobox, TFontCombobox, TPopupMenu, TToolMenuButton } from "../hcl/StdCtrls.js";
import { TDateTime, TFileType, TStream } from "../hcl/System.js";
import { TFrmInsertTable } from "./frmInsertTable.js";
import { THCBarCodeItem } from "./HCBarCodeItem.js";
import { THCCheckBoxItem } from "./HCCheckBoxItem.js";
import { THCComboboxItem } from "./HCComboboxItem.js";
import { HC, THCContentAlign } from "./HCCommon.js";
import { THCDateTimePicker } from "./HCDateTimePicker.js";
import { THCEdit } from "./HCEdit.js";
import { THCEditItem } from "./HCEditItem.js";
import { THCFloatBarCodeItem } from "./HCFloatBarCodeItem.js";
import { THCFloatLineItem } from "./HCFloatLineItem.js";
import { THCImageItem } from "./HCImageItem.js";
import { TParaAlignHorz, TParaLineSpaceMode } from "./HCParaStyle.js";
import { THCRadioGroup } from "./HCRadioGroup.js";
import { THCCustomRectItem } from "./HCRectItem.js";
import { THCStyle } from "./HCStyle.js";
import { THCTableItem } from "./HCTableItem.js";
import { THCFontStyle } from "./HCTextStyle.js";

export class TFrmHCEdit extends TForm {
    constructor(width, height) {
        super(width, height);
        this.state_.add(TControlState.Creating);
        this.captionBar.captureParent = false;
        this.captionBar.controls.clear();

        this.cbbFont = null;
        this.cbbFontSize = null;
        this.cbbFontColor = null;
        this.btnBold = null;
        this.btnItalic = null;
        this.btnUnderline = null;
        this.btnStrikeOut = null;
        this.btnSuperscript = null;
        this.btnSubscript = null;
        this.btnAlignLeft = null;
        this.btnAlignRight = null;
        this.btnAlignCenter = null;
        this.btnAlignJustify = null;
        this.btnAlignScatter = null;

        this._initilization();
        this.state_.delete(TControlState.Creating);
    }

    _openFileDialog() {
        // TOpenDialog.execute(String.format("{0},{1},{2}", TFileType.XML, TFileType.DOCX, ".hcf"), (openDlg) => {
        //     this.FHCEdit.LoadFromFile(openDlg.firstFile);
        // });
    }

    _initilization() {
        let menuInsert = new TPopupMenu();
        menuInsert.dropDownStyle = true;
        menuInsert.addItem("表格").onClick = () => { 
            let vFrmInsertTable = new TFrmInsertTable();
            vFrmInsertTable.showModal(() => {
                this.FHCEdit.InsertTable(vFrmInsertTable.edtRowCount.number, vFrmInsertTable.edtColCount.number);
            });
        }
        
        menuInsert.addItem("图片").onClick = () => {
            TOpenDialog.execute(String.format("{0}", TFileType.IMAGE), (openDlg) => {
                let vTopData = this.FHCEdit.ActiveSectionTopLevelData();
                let vImageItem = new THCImageItem(vTopData);
                vImageItem.LoadGraphicFile(openDlg.firstFile, () => {
                    vImageItem.RestrainSize(vTopData.width, vImageItem.Height);
                    this.FHCEdit.InsertItem(vImageItem);
                });
            });
        }

        menuInsert.addItem("GIF动画");
        
        menuInsert.addItem("横线").onClick = () => {
            this.FHCEdit.InsertLine(1);
        }
        
        let menuControl = menuInsert.addItem("控件");
        menuControl.hint = "插入各类控件";
        menuControl.addItem("CheckBox").onClick = () => {
            let vCheckBoxItem = new THCCheckBoxItem(this.FHCEdit.ActiveSectionTopLevelData(), "勾选框", false);
            this.FHCEdit.InsertItem(vCheckBoxItem);
        }

        menuControl.addItem("Combobox").onClick = () => {
            let vCombobox = new THCComboboxItem(this.FHCEdit.ActiveSectionTopLevelData(), "下拉选择");
            vCombobox.Items.add("选项1");
            vCombobox.Items.add("选项2");
            vCombobox.Items.add("选项3");
            vCombobox.Items.add("选项4");
            vCombobox.Items.add("选项5");
            vCombobox.Items.add("选项6");
            this.FHCEdit.InsertItem(vCombobox);
        }

        menuControl.addItem("DateTimePicker").onClick = () => {
            let vDateTimePicker = new THCDateTimePicker(this.FHCEdit.ActiveSectionTopLevelData(), TDateTime.Now());
            this.FHCEdit.InsertItem(vDateTimePicker);
        }

        menuControl.addItem("RadioGroup").onClick = () => {
            let vHCRadioGroup = new THCRadioGroup(this.FHCEdit.ActiveSectionTopLevelData());
            vHCRadioGroup.AddItem("选项1");
            vHCRadioGroup.AddItem("选项2");
            vHCRadioGroup.AddItem("选项3");
            this.FHCEdit.InsertItem(vHCRadioGroup);
        }

        menuControl.addItem("Edit").onClick = () => {
            let vEditItem = new THCEditItem(this.FHCEdit.ActiveSectionTopLevelData(), "文本框");
            this.FHCEdit.InsertItem(vEditItem);
        }
        
        menuInsert.addItem("分页").onClick = () => {
            this.FHCEdit.InsertPageBreak();
        }

        menuInsert.addItem("文本").onClick = () => {
            this.FHCEdit.InsertText("这是InsertText插入的内容^_^");
        }

        let menuCode = menuInsert.addItem("条码");
        menuCode.addItem("一维码").onClick = () => {
            let vBarCodeItem = new THCBarCodeItem(this.FHCEdit.ActiveSectionTopLevelData(), "123");
            this.FHCEdit.InsertItem(vBarCodeItem);
        }

        menuCode.addItem("二维码");
        
        let menuFloat = menuInsert.addItem("浮动对象");
        menuFloat.addItem("直线").onClick = () => {
            let vFloatLineItem = new THCFloatLineItem(this.FHCEdit.ActiveSection.ActiveData);
            this.FHCEdit.InsertFloatItem(vFloatLineItem);
        }

        menuFloat.addItem("一维码").onClick = () => {
            let vFloatBarCodeItem = new THCFloatBarCodeItem(this.FHCEdit.ActiveSection.ActiveData);
            this.FHCEdit.InsertFloatItem(vFloatBarCodeItem);
        }
        
        menuInsert.addItem("超链接").onClick = () => {
            let vTopData = this.FHCEdit.ActiveSectionTopLevelData();
            let vTextItem = vTopData.CreateDefaultTextItem();
            vTextItem.Text = "打开百度";
            vTextItem.HyperLink = "https://www.baidu.com";
            this.FHCEdit.InsertItem(vTextItem);
        }
        
        menuInsert.addItem("域").onClick = () => {
            this.FHCEdit.InsertDomain(null);
        }

        let menuFile = new TPopupMenu();
        menuFile.dropDownStyle = true;
        menuFile.addItem("打开").onClick = () => { 
            this._openFileDialog();
        }

        menuFile.addItem("导出").onClick = () => {
            let vStream = new TStream();
            this.FHCEdit.SaveToStream(vStream, false);
            let vByteData = new Uint8Array(vStream.buffer);
            let vBlob = new Blob([vByteData], {type:"application/octet-stream"});
            //let vBlob = new Blob(vStream.buffer, {type:"application/octet-stream"});
            let vDownloadUrl = window.URL.createObjectURL(vBlob);
            let vAnchor = document.createElement("a");
            vAnchor.href = vDownloadUrl;
            vAnchor.download = "HCView H5.hcf";
            vAnchor.click();
            window.URL.revokeObjectURL(vBlob);
            document.removeChild(vAnchor);
        }

        let btnFile = new TToolMenuButton("文件");
        btnFile.image.src = "../image/file.png";
        btnFile.textVisible = true;
        btnFile.hint = "文档处理";
        btnFile.dropDownMenu = menuInsert;
        this.captionBar.addControl(btnFile);
        
        this.captionBar.addButton("新建", false, "../image/new.png").onClick = () => {
            this.FHCEdit.clear();
        }
        
        this.captionBar.addButton("保存", false, "../image/save.png").onClick = () => { this.DoSave(); }

        this.captionBar.addButton("打印", false, "../image/print.png").onClick = () => { this.DoPrint(); };
        this.captionBar.addSpliter();
        this.captionBar.addButton("撤销", false, "../image/undo.png").onClick = () => { this.FHCEdit.Undo(); }
        this.captionBar.addButton("恢复", false, "../image/redo.png").onClick = () => { this.FHCEdit.Redo(); }
        this.captionBar.addSpliter();

        let btnInsert = new TToolMenuButton("插入");
        btnInsert.textVisible = true;
        btnInsert.hint = "插入功能";
        btnInsert.dropDownMenu = menuInsert;
        this.captionBar.addControl(btnInsert);

        this.cbbFont = new TFontCombobox("");
        this.cbbFont.static = true;
        this.cbbFont.align = TAlign.Left;
        this.cbbFont.onSelectedIndexChange = () => { this.FHCEdit.ApplyTextFontName(this.cbbFont.text); }
        this.captionBar.addControl(this.cbbFont);
        this.cbbFontSize = new TCombobox("");
        this.cbbFontSize.static = true;
        this.cbbFontSize.width = 48;
        this.cbbFontSize.dropDownWidth = 64;
        this.cbbFontSize.state = true;
        this.cbbFontSize.align = TAlign.Left;
        this.cbbFontSize.addItem("初号");
        this.cbbFontSize.addItem("小初");
        this.cbbFontSize.addItem("一号");
        this.cbbFontSize.addItem("小一");
        this.cbbFontSize.addItem("二号");
        this.cbbFontSize.addItem("小二");
        this.cbbFontSize.addItem("三号");
        this.cbbFontSize.addItem("小三");
        this.cbbFontSize.addItem("四号");
        this.cbbFontSize.addItem("小四");
        this.cbbFontSize.addItem("五号");
        this.cbbFontSize.addItem("小五");
        this.cbbFontSize.addItem("六号");
        this.cbbFontSize.addItem("小六");
        this.cbbFontSize.addItem("七号");
        this.cbbFontSize.addItem("八号");
        this.cbbFontSize.addItem("5");
        this.cbbFontSize.addItem("5.5");
        this.cbbFontSize.addItem("6.5");
        this.cbbFontSize.addItem("7.5");
        this.cbbFontSize.addItem("8");
        this.cbbFontSize.addItem("9");
        this.cbbFontSize.addItem("10");
        this.cbbFontSize.addItem("10.5");
        this.cbbFontSize.addItem("11");
        this.cbbFontSize.addItem("12");
        this.cbbFontSize.addItem("14");
        this.cbbFontSize.addItem("16");
        this.cbbFontSize.addItem("18");
        this.cbbFontSize.addItem("20");
        this.cbbFontSize.addItem("22");
        this.cbbFontSize.addItem("24");
        this.cbbFontSize.addItem("26");
        this.cbbFontSize.addItem("28");
        this.cbbFontSize.addItem("36");
        this.cbbFontSize.addItem("48");
        this.cbbFontSize.addItem("72");
        this.cbbFontSize.itemIndex = 10;
        this.cbbFontSize.onSelectedIndexChange = () => { this.FHCEdit.ApplyTextFontSize(TFont.fontSizeToPt(this.cbbFontSize.text)); }
        this.captionBar.addControl(this.cbbFontSize);
        
        this.cbbFontColor = new TColorCombobox();
        this.cbbFontColor.align = TAlign.Left;
        this.cbbFontColor.onSelectedIndexChange = () => { this.FHCEdit.ApplyTextColor(this.cbbFontColor.color); }
        this.captionBar.addControl(this.cbbFontColor);
        this.captionBar.addSpliter();
        this.btnBold = this.captionBar.addButton("加粗", false, "../image/bold.png");
        this.btnBold.hint = "加粗";
        this.btnBold.onClick = () => { this.FHCEdit.ApplyTextStyle(THCFontStyle.Bold); }
        
        this.btnItalic = this.captionBar.addButton("倾斜", false, "../image/italic.png");
        this.btnItalic.hint = "倾斜";
        this.btnItalic.onClick = () => { this.FHCEdit.ApplyTextStyle(THCFontStyle.Italic); }
        
        this.btnUnderline = this.captionBar.addButton("下划线", false, "../image/underline.png");
        this.btnUnderline.hint = "下划线";
        this.btnUnderline.onClick = () => { this.FHCEdit.ApplyTextStyle(THCFontStyle.Underline); }
        
        this.btnStrikeOut = this.captionBar.addButton("中划线", false, "../image/strikeout.png");
        this.btnStrikeOut.hint = "中划线";
        this.btnStrikeOut.onClick = () => { this.FHCEdit.ApplyTextStyle(THCFontStyle.StrikeOut); }
        
        this.btnSuperscript = this.captionBar.addButton("上标", false, "../image/superscript.png");
        this.btnSuperscript.hint = "上标";
        this.btnSuperscript.onClick = () => { this.FHCEdit.ApplyTextStyle(THCFontStyle.Superscript); }
        
        this.btnSubscript = this.captionBar.addButton("下标", false, "../image/subscript.png");
        this.btnSubscript.hint = "下标";
        this.btnSubscript.onClick = () => { this.FHCEdit.ApplyTextStyle(THCFontStyle.Subscript); }
        
        this.captionBar.addSpliter();
        let btnLeftIdent = this.captionBar.addButton("左缩进", false, "../image/rightindent.png");
        btnLeftIdent.hint = "增加左缩进";
        btnLeftIdent.onClick = () => { this.FHCEdit.ApplyParaLeftIndentAdd(); }
        
        let btnRightIdent = this.captionBar.addButton("右缩进", false, "../image/leftindent.png");
        btnRightIdent.hint = "减少左缩进";
        btnRightIdent.onClick = () => { this.FHCEdit.ApplyParaLeftIndentAdd(false); }
        
        this.btnAlignLeft = this.captionBar.addButton("居左", false, "../image/left.png");
        this.btnAlignLeft.hint = "左对齐";
        this.btnAlignLeft.onClick = () => { this.FHCEdit.ApplyParaAlignHorz(TParaAlignHorz.Left); }
        
        this.btnAlignCenter = this.captionBar.addButton("居中", false, "../image/center.png");
        this.btnAlignCenter.hint = "居中对齐";
        this.btnAlignCenter.onClick = () => { this.FHCEdit.ApplyParaAlignHorz(TParaAlignHorz.Center); }
        
        this.btnAlignRight = this.captionBar.addButton("居右", false, "../image/right.png");
        this.btnAlignRight.hint = "右对齐";
        this.btnAlignRight.onClick = () => { this.FHCEdit.ApplyParaAlignHorz(TParaAlignHorz.Right); }
        
        this.btnAlignJustify = this.captionBar.addButton("两端", false, "../image/justify.png");
        this.btnAlignJustify.hint = "两端对齐";
        this.btnAlignJustify.onClick = () => { this.FHCEdit.ApplyParaAlignHorz(TParaAlignHorz.Justify); }
        
        this.btnAlignScatter = this.captionBar.addButton("分散", false, "../image/scatter.png");
        this.btnAlignScatter.hint = "分散对齐";
        this.btnAlignScatter.onClick = () => { this.FHCEdit.ApplyParaAlignHorz(TParaAlignHorz.Scatter); }
        
        let menuLineSpace = new TPopupMenu();
        menuLineSpace.addItem("单倍").onClick = () => { this.FHCEdit.ApplyParaLineSpace(TParaLineSpaceMode.PLS100); }
        menuLineSpace.addItem("1.15倍").onClick = () => { this.FHCEdit.ApplyParaLineSpace(TParaLineSpaceMode.PLS115); }
        menuLineSpace.addItem("1.5倍").onClick = () => { this.FHCEdit.ApplyParaLineSpace(TParaLineSpaceMode.PLS150); }
        menuLineSpace.addItem("2倍").onClick = () => { this.FHCEdit.ApplyParaLineSpace(TParaLineSpaceMode.PLS200); }
        menuLineSpace.addItem("固定值");
        
        let menuBtnLineSpace = new TToolMenuButton("行间距");
        menuBtnLineSpace.hint = "行间距选项";
        menuBtnLineSpace.image.src = "../image/linespace.png";
        menuBtnLineSpace.dropDownMenu = menuLineSpace;
        this.captionBar.addControl(menuBtnLineSpace);
        
        let viewPopup = new TPopupMenu();
        viewPopup.onPupup = () => {
            let vActiveData = this.FHCEdit.ActiveSection.ActiveData;
            let vActiveFloatItem = vActiveData.GetActiveFloatItem();
            let vReadOnly = vActiveData.ReadOnly;
          
            if (vActiveFloatItem != null) {
                for (let i = 0; i < viewPopup.subItems.count; i++)
                    viewPopup.subItems[i].visible = false;

                if (!vReadOnly) {
                    mniFloatItemProperty.visible = this.FHCEdit.DesignModeEx;
                    if (vActiveFloatItem.isClass(THCFloatBarCodeItem))
                        mniFloatItemProperty.Caption = "浮动条码";
                }
          
                return;
            } else
              mniFloatItemProperty.visible = false;

            let vActiveItem = vActiveData.GetActiveItem();
            let vTopData = null;
            let vTopItem = vActiveItem;

            while (vTopItem != null && vTopItem.isClass(THCCustomRectItem)) {
                if (vTopItem.GetTopLevelData() != null) {
                    if (vTopData != null) {
                        vActiveData = vTopData;
                        vActiveItem = vTopItem;
                    }

                    vTopData = vTopItem.GetTopLevelData();
                    vTopItem = vTopData.GetActiveItem();
                } else
                    break;
            }

            if (vTopData == null)
                vTopData = vActiveData;

            tableMenu.visible = vActiveItem.StyleNo == THCStyle.Table;
            if (tableMenu.visible) {
                let vTableItem = vActiveItem;
                mniInsertRowTop.enabled = vTableItem.GetEditCell() != null;
                mniInsertRowBottom.enabled = mniInsertRowTop.enabled;
                mniInsertColLeft.enabled = mniInsertRowTop.enabled;
                mniInsertColRight.enabled = mniInsertRowTop.enabled;
                mniSplitRow.enabled = mniInsertRowTop.enabled;
                mniSplitCol.enabled = mniInsertRowTop.enabled;

                mniDeleteCurRow.enabled = vTableItem.CurRowCanDelete();
                mniDeleteCurCol.enabled = vTableItem.CurColCanDelete();
                mniMerge.enabled = vTableItem.SelectedCellCanMerge();

                if (vTableItem.BorderVisible)
                    mniDisBorder.text = "隐藏边框";
                else
                    mniDisBorder.text = "显示边框";
            }

            mniCut.enabled = (!this.FHCEdit.ActiveSection.ReadOnly) && vTopData.SelectExists();
            mniCopy.enabled = mniCut.enabled;
            mniPaste.enabled = (!this.FHCEdit.ActiveSection.ReadOnly)
                && (clipboard.hasFormat(HC.HCExtFormat) || clipboard.hasFormat(TDataFormat.UnicodeText));
        }

        let mniCut = viewPopup.addItem("剪切 Ctrl+X");
        mniCut.onClick = () => {
            this.FHCEdit.Cut();
        }

        let mniCopy = viewPopup.addItem("复制 Ctrl+C");
        mniCopy.onClick = () => {
            this.FHCEdit.Copy();
        }

        let mniPaste = viewPopup.addItem("粘贴 Ctrl+V");
        mniPaste.onClick = () => {
            this.FHCEdit.Paste();
        }

        let tableMenu = viewPopup.addItem("表格");
        let mniInsertRowTop = tableMenu.addItem("上方插入行");
        mniInsertRowTop.onClick = () => {
            this.FHCEdit.ActiveTableInsertRowBefor(1);
        }

        let mniInsertRowBottom = tableMenu.addItem("下方插入行");
        mniInsertRowBottom.onClick = () => {
            this.FHCEdit.ActiveTableInsertRowAfter(1);
        }

        tableMenu.addSpliter();
        let mniInsertColLeft = tableMenu.addItem("左侧插入列");
        mniInsertColLeft.onClick = () => {
            this.FHCEdit.ActiveTableInsertColBefor(1);
        }

        let mniInsertColRight = tableMenu.addItem("右侧插入列");
        mniInsertColRight.onClick = () => {
            this.FHCEdit.ActiveTableInsertColAfter(1);
        }

        tableMenu.addSpliter();
        let mniMerge = tableMenu.addItem("合并单元格");
        mniMerge.onClick = () => {
            this.FHCEdit.MergeTableSelectCells();
        }

        let cellAlign = tableMenu.addItem("对齐方式");
        cellAlign.addItem("顶部左").onClick = () => {
            this.FHCEdit.ApplyTableCellAlign(THCContentAlign.TopLeft);
        }

        cellAlign.addItem("顶部居中").onClick = () => {
            this.FHCEdit.ApplyTableCellAlign(THCContentAlign.TopCenter);
        }

        cellAlign.addItem("顶部右").onClick = () => {
            this.FHCEdit.ApplyTableCellAlign(THCContentAlign.TopRight);
        }

        cellAlign.addItem("居中左").onClick = () => {
            this.FHCEdit.ApplyTableCellAlign(THCContentAlign.CenterLeft);
        }

        cellAlign.addItem("居中居中").onClick = () => {
            this.FHCEdit.ApplyTableCellAlign(THCContentAlign.CenterCenter);
        }

        cellAlign.addItem("居中右").onClick = () => {
            this.FHCEdit.ApplyTableCellAlign(THCContentAlign.CenterRight);
        }

        cellAlign.addItem("底部左").onClick = () => {
            this.FHCEdit.ApplyTableCellAlign(THCContentAlign.BottomLeft);
        }

        cellAlign.addItem("底部居中").onClick = () => {
            this.FHCEdit.ApplyTableCellAlign(THCContentAlign.BottomCenter);
        }

        cellAlign.addItem("底部右").onClick = () => {
            this.FHCEdit.ApplyTableCellAlign(THCContentAlign.BottomRight);
        }

        tableMenu.addSpliter();
        let mniSplitRow = tableMenu.addItem("拆分行");
        mniSplitRow.onClick = () => {
            this.FHCEdit.ActiveTableSplitCurRow();
        }

        let mniSplitCol = tableMenu.addItem("拆分列");
        mniSplitCol.onClick = () => {
            this.FHCEdit.ActiveTableSplitCurCol();
        }

        tableMenu.addSpliter();
        let mniDeleteCurRow = tableMenu.addItem("删除当前行");
        mniDeleteCurRow.onClick = () => {
            this.FHCEdit.ActiveTableDeleteCurRow();
        }

        let mniDeleteCurCol = tableMenu.addItem("删除当前列");
        mniDeleteCurCol.onClick = () => {
            this.FHCEdit.ActiveTableDeleteCurCol();
        }

        tableMenu.addSpliter();
        tableMenu.addItem("边框及背景").onClick = () => {
            //let vFrmBorderBackColor = new TfrmBorderBackColor();
            //vFrmBorderBackColor.SetView(this.FHCEdit);
        }

        let mniDisBorder = tableMenu.addItem("显示/隐藏边框");
        mniDisBorder.onClick = () => {
            let vTable = this.FHCEdit.ActiveSection.ActiveData.GetActiveItem();
            if (vTable.isClass(THCTableItem)) {
                vTable.BorderVisible = !vTable.BorderVisible;
                this.FHCEdit.UpdateView();
            }
        }

        tableMenu.addItem("属性").onClick = () => {
            this.DoTablePropertyClick();
        }
        
        viewPopup.addItem("段落").onClick = () => {
            //let vFrmParagraph = new TfrmParagraph();
            //vFrmParagraph.SetView(this.FHCEdit);
        }

        let mniControlItem = viewPopup.addItem("属性");
        
        let mniFloatItemProperty = viewPopup.addItem("浮动对象属性");
        mniFloatItemProperty.onClick = () => {
        
        }

        this.FHCEdit = new THCEdit();
        this.FHCEdit.align = TAlign.Client;
        this.FHCEdit.popupMenu = viewPopup;
        this.FHCEdit.OnCaretChange = () => { this.DoCaretChange(); }
        this.addControl(this.FHCEdit);
    }

    DoCaretChange() {
        this.CurTextStyleChange(this.FHCEdit.CurStyleNo);
        this.CurParaStyleChange(this.FHCEdit.CurParaNo);
    }

    CurTextStyleChange(styleNo) {
        if (styleNo >= 0) {
            let vTextStyle = this.FHCEdit.Style.TextStyles[styleNo];
            this.cbbFont.itemIndex = this.cbbFont.items.indexOf(vTextStyle.Family);
            this.cbbFontSize.itemIndex = this.cbbFontSize.items.indexOf(TFont.fontPtToSize(vTextStyle.Size));
            this.cbbFontColor.color = vTextStyle.Color;
            this.btnBold.checked = vTextStyle.FontStyles.has(TFontStyle.Bold);
            this.btnItalic.checked = vTextStyle.FontStyles.has(TFontStyle.Italic);
            this.btnUnderline.checked = vTextStyle.FontStyles.has(TFontStyle.Underline);
            this.btnStrikeOut.checked = vTextStyle.FontStyles.has(TFontStyle.StrikeOut);
            this.btnSuperscript.checked = vTextStyle.FontStyles.has(TFontStyle.SuperScript);
            this.btnSubscript.checked = vTextStyle.FontStyles.has(TFontStyle.SubScript);
        } else {
            this.btnBold.checked = false;
            this.btnItalic.checked = false;
            this.btnUnderline.checked = false;
            this.btnStrikeOut.checked = false;
            this.btnSuperscript.checked = false;
            this.btnSubscript.checked = false;
        }
    }
    
    CurParaStyleChange(paraNo) {
        if (paraNo >= 0) {
            let vAlignHorz = this.FHCEdit.Style.ParaStyles[paraNo].AlignHorz;
    
            this.btnAlignLeft.checked = vAlignHorz == TParaAlignHorz.Left;
            this.btnAlignRight.checked = vAlignHorz == TParaAlignHorz.Right;
            this.btnAlignCenter.checked = vAlignHorz == TParaAlignHorz.Center;
            this.btnAlignJustify.checked = vAlignHorz == TParaAlignHorz.Justify;
            this.btnAlignScatter.checked = vAlignHorz == TParaAlignHorz.Scatter;
        }
    }
}

application.icon.src = "../image/hcview.png";
let mainForm = new TFrmHCEdit(hcl.width, hcl.height);
application.addForm(mainForm);
hcl.homePath = "../hcl/";
hcl.autoWidth = true;
application.run();